<?php
/**
 * ?do=process待办事项已受理
 */
apf_require_controller("Abstract");
apf_require_class("Util_MultiPage");
apf_require_class("Bll_Flow_Info");
apf_require_class("Bll_Hr_Property");
apf_require_class("Bll_Flow_Transfer");
apf_require_class("Bll_Flow_Dimission");
apf_require_class("Bll_Flow_Recruit");
apf_require_class("Bll_Flow_UserRegular");
apf_require_class("Bll_Flow_UserRenew");

class Flow_ListController extends AbstractController {

    const PAGE_ROWS = 10; //每页显示10条
    const PAGE_NUM_SIZE = 10; //翻页工具条显示10个页码链接

    private $is_ceo=0;
    public function handle_request() {
        $params = $this->request->get_parameters();
        if(empty($params['do'])){
            $do = 'list';
        }else{
            $do = $params['do'];
        }
        if(!in_array($do,array('list','process','all'))){
            $do = 'list';
        }
        $page = intval($params['p']);
        if($this->request->is_post_method()){
            $page = 1;
        }
        $page = $page<1?1:$page;
        $offset = ($page-1)*self::PAGE_ROWS;

        $_where = $this->_build_uri_params($params);
        if(!empty($_where['start']) || !empty($_where['end'])){
            if($_where['end'] < $_where['start']){
                $this->show_tips('ERROR_SATRT_DATE');
            }
        }
        $_where['role_list'] = $this->user_info['role_list'];
        $_where['t_role'] = $this->_build_last_role();
        $this->is_ceo = !empty($this->user_info['role_list'][Const_Flow::ROLE_CEO]) ? 1 : 0;
        $this->flow_bll = new Bll_Flow_Info();

        $data = $this->_build_list($do, $_where, self::PAGE_ROWS, $offset);
        $userinfo_list = $this->_build_user_info($data['datalist']);

        $data['t_role'] = $this->_build_last_role();//流程监控

        $data['multipage']='';
        //分页链接不需要这个参数
        unset($_where['role_list']);
        unset($_where['user_id']);
        unset($_where['t_role']);
        $_where['do'] = $do;
        $_where['p'] = $page;
        $url_suffix = "";
        $url_prefix = Flow_Urls::flow_list();
        if($data['count'] > self::PAGE_ROWS){
            $data['multipage'] = Util_MultiPage::multiPage($url_prefix, $_where, $url_suffix, $data['count'], self::PAGE_ROWS, self::PAGE_NUM_SIZE, false, true);
        }
        $params['do'] = $do;

        $data['citylist'] = $this->_get_city_list();
        $data['flow_type_list'] = $this->_get_flow_type();
        //flow_type
        if($params['flow_type']){
            $params['flow_type'] = $params['flow_type']==Const_Flow::FLOW_TYPE_MONEY ? Const_Flow::FLOW_TYPE_TRANSFER : $params['flow_type'];
        }
        $is_hr_admin = $this->check_role_auth();

        $this->request->set_attribute('userinfo_list', $userinfo_list);
        $this->request->set_attribute('is_hr_admin', $is_hr_admin);
        $this->request->set_attribute('data', $data);
        $this->request->set_attribute('params', $params);
        $this->request->set_attribute('role_list', $this->user_info['role_list']);
        return 'Flow_List';
    }
    /**
     * 得到最终的角色 it or hr
     */
    private function _build_last_role(){
        $t_role = array();
        if(isset($this->user_info['role_list'][Const_Flow::ROLE_HR])){
            $t_role[Const_Flow::ROLE_HR] = 1;
        }
        if(isset($this->user_info['role_list'][Const_Flow::ROLE_HRD])){
            $t_role[Const_Flow::ROLE_HRD] = 1;
        }
        //只给上海hr招聘开权限
        if(isset($this->user_info['role_list'][Const_Flow::ROLE_RECRUIT])){
            $t_role[Const_Flow::ROLE_RECRUIT] = 1;
        }
        if(isset($this->user_info['role_list'][Const_Flow::ROLE_IT])){
            $t_role[Const_Flow::ROLE_IT] = 1;
        }
        return $t_role;
    }
    /**
     * 当前阶段还能操作的内容,格式化，对于招聘需要特殊处理
     * @param sting $data
     */
    private function _build_opt_list($flow_role,$flow_data){
        $str = array();
        if(empty($flow_role)){
            return '';
        }
        $role_ids = explode(',',$flow_role);
        $role_list = Const_Flow::role_list();
        foreach($role_ids as $id){
            if($flow_data['flow_type']==Const_Flow::FLOW_TYPE_RECRUIT &&  $id==Const_Flow::ROLE_SUPERIOR){
                $str[] = $this->_show_recruit_stage($flow_data);
            }else{
                $str[] = $id==Const_Flow::ROLE_STAFF ? '交接资料' : $role_list[$id];
            }
        }
        return implode('-',$str);
    }
    /**
     * build url
     * @param sting $do list|process
     * @param array $data
     * @return string
     */
    private function _build_url($do,$data,$flow_data,$opt_user_id){
        $flow_type = $data['flow_type'];
        $is_done = $do;
        $url = '';
        $flow_status = $flow_data['now_status'];//流程状态
        switch ($flow_type){
            case Const_Flow::FLOW_TYPE_ENTRY:
                $url = Flow_Urls::add_staff($data['user_id']);
                if($is_done=='process'){
                    if($flow_data['flow_opt'] == Const_Flow::PHASE_1 && $opt_user_id==$this->user_info['user_id']){
                        $url = Flow_Urls::add_user($data['user_id']);
                    }
                }
                break;
            case Const_Flow::FLOW_TYPE_TRANSFER:
            case Const_Flow::FLOW_TYPE_MONEY:
                $url = Flow_Urls::transfer().'?flow_id='.$data['flow_id'];
                if($is_done=='process' && $flow_status==Const_Flow::FLOW_STATUS_DOING){
                    if($flow_data['flow_opt'] == Const_Flow::PHASE_11){
                        $url = Flow_Urls::apply_transfer().'?flow_id='.$data['flow_id'];
                    }else{
                        //本来要做ceo和一级申请人主管跳级的,改在流程页做跳转
                    }
                    $url .='&view=1';//说明是来编辑的
                }
                break;
            case Const_Flow::FLOW_TYPE_DIMISSION:
                $url = Flow_Urls::dimission().'?flow_id='.$data['flow_id'];
                if($is_done=='process' && $flow_status==Const_Flow::FLOW_STATUS_DOING){
                    if($flow_data['flow_opt'] == Const_Flow::PHASE_29){
                        $url = Flow_Urls::apply_retire().'?flow_id='.$data['flow_id'];
                    }elseif($flow_data['flow_opt'] == Const_Flow::PHASE_28){
                        $url = Flow_Urls::to_dimiss().'?flow_id='.$data['flow_id'];
                    }
                    $url .='&view=1';
                }
                break;
            case Const_Flow::FLOW_TYPE_RECRUIT:
                $url = Flow_Urls::flow_recruitment($data['flow_id']);
                if($is_done=='process' && $flow_status==Const_Flow::FLOW_STATUS_DOING){
                    if($flow_data['flow_opt'] == Const_Flow::PHASE_50 || ($flow_data['flow_opt'] == Const_Flow::PHASE_76 && $this->is_ceo && $flow_data['user_id']==6932)){
                        if($flow_status==1){
                            $url = Flow_Urls::flow_apply_recruit().'?flow_id='.$data['flow_id'];
                        }
                    }
                    $url .='&view=1';
                }
                break;
            case Const_Flow::FLOW_TYPE_REGULAR:
                $url = Flow_Urls::flow_regular($data['flow_id']);
                if($is_done=='process'){
                    $url .='&view=1';
                }
                break;
            case Const_Flow::FLOW_TYPE_RENEW:
                $url = Flow_Urls::flow_renew($data['flow_id']);
                if($is_done=='process'){
                    $url .='&view=1';
                }
                break;

            default:
                break;
        }
        return $url;
    }
    /**
     * 用户职位数据,取出页面中可以显示的用户字段信息
     * @param array $datalist
     * @return
     */
    private function _build_user_info($datalist){
        if(empty($datalist)){
            return array();
        }
        $uids = $tmp_uids = $new_uids = array();
        $user_data = array();
        $is_transfer = $is_dimission = 0;
        foreach ($datalist as $val){
            if($val['flow_type']==1){
                $new_uids[$val['user_id']] = $val['flow_id'];
            }else{
                if($val['flow_type']==3){
                    $is_dimission = 1;
                }elseif($val['flow_type']==5){
                    $is_recruit = 1;
                }elseif($val['flow_type']==2 || $val['flow_type']==4){
                    $is_transfer = 1;
                }elseif($val['flow_type']==6 ){
                    $is_regular = 1;
                }elseif($val['flow_type']==7){
                    $is_renew = 1;
                }
                $tmp_uids[$val['flow_id']] = $val['user_id'];
            }
        }
        $job_ids = array();
        if($is_dimission){//是否有离职信息
            $bll_dimiss = new Bll_Flow_Dimission();
            $d_flow_ids = array_keys($tmp_uids);
            $dimiss = $bll_dimiss->find_by_flow_ids($d_flow_ids);
            foreach($tmp_uids as $flow_id=>$uid){
                if(!empty($dimiss[$flow_id])){
                    $job_ids[$flow_id] = $dimiss[$flow_id]['job_id'];
                }
            }
        }
        if($is_transfer){//是否有转岗信息
            $bll_tran = new Bll_Flow_Transfer();
            $t_flow_ids = array_keys($tmp_uids);
            $tran = $bll_tran->find_by_ids($t_flow_ids);
            foreach($tmp_uids as $flow_id=>$uid){
                if(!empty($tran[$flow_id])){
                    $job_ids[$flow_id] = $tran[$flow_id]['old_job_id'];
                }
            }
        }

        if($is_recruit){
            $bll_recruit = new Bll_Flow_Recruit();
            $r_flow_ids = array_keys($tmp_uids);
            $recruit = $bll_recruit->get_recruit_by_ids($r_flow_ids);
            foreach($tmp_uids as $flow_id=>$uid){
                if(!empty($recruit[$flow_id])){
                    $job_ids[$flow_id] = $recruit[$flow_id]['job_id'];
                }
            }
        }
        if($is_regular){//是否有转岗信息
            $bll_regular = new Bll_Flow_UserRegular();
            $rg_flow_ids = array_keys($tmp_uids);
            $d_regular = $bll_regular->find_by_ids($rg_flow_ids);
            foreach($tmp_uids as $flow_id=>$uid){
                if(!empty($d_regular[$flow_id])){
                    $job_ids[$flow_id] = $d_regular[$flow_id]['job_id'];
                }
            }
        }

        if($is_renew){
            $bll_renew = new Bll_Flow_UserRenew();
            $rn_flow_ids = array_keys($tmp_uids);
            $d_renew = $bll_renew->find_by_ids($rn_flow_ids);
            foreach($tmp_uids as $flow_id=>$uid){
                if(!empty($d_renew[$flow_id])){
                    $job_ids[$flow_id] = $d_renew[$flow_id]['job_id'];
                }
            }
        }
        $bll = new Bll_Hr_OutInterface();
        if(!empty($tmp_uids)){
            $uids = array_values($tmp_uids);
            $tmp_user_data = $bll->get_user_by_ids($uids,0);
            foreach($tmp_uids as $flow_id=>$uid){
                $user_data[$flow_id]['code'] = $tmp_user_data[$uid]['code'];
                $user_data[$flow_id]['user_id'] = $tmp_user_data[$uid]['user_id'];
                $user_data[$flow_id]['name'] = $tmp_user_data[$uid]['name'];
            }
        }

        if(!empty($job_ids)){//获取职位信息
            $jobids = array_values($job_ids);
            $job_list = $bll->get_job_dep_infos($jobids);
            foreach($job_ids as $flow_id=>$jobid){
                $user_data[$flow_id]['job_name'] = $job_list[$jobid]['position_name'];
                $user_data[$flow_id]['department_id'] = $job_list[$jobid]['department_id'];
                $user_data[$flow_id]['department_name'] = $job_list[$jobid]['bu_name'].$job_list[$jobid]['department_name'];
                $user_data[$flow_id]['city_name']=$job_list[$jobid]['city_name'];
            }
        }

        if(!empty($new_uids)){//新入职用户,这里会有取消入职
            apf_require_class('Bll_Flow_User');
            $user_bll = new Bll_Flow_User();
            $new_a_uids = array_keys($new_uids);
            $new_user = $user_bll->get_job_by_uids($new_a_uids);
            foreach($new_user as $uid=>$val){
                $flow_id = $new_uids[$uid];
                $user_data[$flow_id] = $val;
            }
        }
        return $user_data;
    }
    /**
     *
     * @param array $params
     * @return multitype:number string NULL
     */
    private function _build_uri_params($params){
        $_uri_params=array();
        if(!isset($params['user_id'])){
            $_uri_params['user_id'] = $this->user_info['user_id'];
        }
        if(!empty($params['city_id'])){
            $_uri_params['city_id'] = intval($params['city_id']);
        }
        if(!empty($params['start']) || !empty($params['end'])){
            $start = $params['start'] ? date('Ymd',strtotime($params['start'])) : 0;
            $end = $params['end'] ? date('Ymd',strtotime($params['end'])) : 0;

            $_uri_params['start'] = $start ? intval($start) : 1;//如果只搜索结束时间
            $_uri_params['end'] = $end ? intval($end) : 2100000000;//如果只搜索开始时间
        }
        if(!empty($params['flow_type'])){
            $_uri_params['flow_type'] = intval($params['flow_type']);
        }
        if(!empty($params['now_status'])){
            $_uri_params['now_status'] = trim($params['now_status']);
        }
        if(!empty($params['user_name'])){
            $_uri_params['user_name'] = trim($params['user_name']);
        }
        return $_uri_params;
    }
    /**
     * 待处理
     * @param array $params
     * @param int $limit
     * @param int $offset
     */
    private function _build_list($do, $params, $limit, $offset){
        $role_list = array();
        if($do=='list' || $do=='all'){//已处理读的是log表
            if(!empty($params['flow_type'])){
                $params['flow_type'] = intval($params['flow_type']);
                if($params['flow_type'] == Const_Flow::FLOW_TYPE_TRANSFER){
                    $params['flow_type'] = array(Const_Flow::FLOW_TYPE_TRANSFER,Const_Flow::FLOW_TYPE_MONEY);
                }else{
                    $params['flow_type'] = array($params['flow_type']);
                }
            }
        }
        if($do == 'list'){
            $data = $this->flow_bll->get_all_flow_list($params,$limit,$offset);
        }elseif($do=='process'){//已办理列表
            $bll = new Bll_Flow_OperateLog();
            $data = $bll->get_flow_done_list($params,$limit,$offset);
            //批量得到flow_id
            $flow_ids = array();
            if(!empty($data['datalist'])){
                foreach($data['datalist'] as $k=>$val){
                    $flow_ids[] = $val['flow_id'];
                }
                $flow_ids = array_unique($flow_ids);
                $flow_data = $this->flow_bll->find_by_ids($flow_ids);
                foreach($flow_data as $val){
                    $role_list[$val['flow_id']] = $val;
                }
            }
        }elseif($do=='all'){//根据角色  上海hr可以查看所有的流程 ,it能看全国的,招聘能看全国的。但会跟it有权限叠加
            $all_role = $this->_build_last_role();
            if(isset($all_role[Const_Flow::ROLE_HR]) || isset($all_role[Const_Flow::ROLE_HRD])){
                $data = $this->flow_bll->get_hr_role_list($params,$limit,$offset);
            }elseif (isset($all_role[Const_Flow::ROLE_IT])){
                if(empty($all_role[Const_Flow::ROLE_RECRUIT])){
                    $data = $this->flow_bll->get_it_role_list($params,$limit,$offset);
                }else{//权限叠加了
                    $params['role_add']=Const_Flow::ROLE_IT;
                    $data = $this->flow_bll->get_recruit_role_list($params,$limit,$offset);
                }
            }elseif (isset($all_role[Const_Flow::ROLE_RECRUIT])){
                $data = $this->flow_bll->get_recruit_role_list($params,$limit,$offset);
            }
        }
        if(!empty($data['datalist'])){
            foreach($data['datalist'] as $k=>$val){
                $flow_data = empty($role_list[$val['flow_id']]) ? array() : $role_list[$val['flow_id']];
                $opt_user_id=0;//操作人id
                if($do=='list' || $do=='all'){
                    $flow_role_list = $val['flow_role_list'];
                    $flow_data = $val;
                }elseif($do=='process'){
                    $flow_role_list = $flow_data['flow_role_list'];
                    $data['datalist'][$k]['now_status'] = $flow_data['now_status'];
                    $flow_data = $role_list[$val['flow_id']];
                    $opt_user_id = $val['opt_user_id'];
                }
                $data['datalist'][$k]['url'] = $this->_build_url($do,$val,$flow_data,$opt_user_id);

                $data['datalist'][$k]['role_list'] = $this->_build_opt_list($flow_role_list,$flow_data);
                $data['datalist'][$k]['de_url'] = $this->_get_flow_url($val['flow_type'],$val['flow_id'],$val['user_id']);
            }
        }

        return $data;
    }
    /**
     * 主要是撤消的情况
     * @param int $flow_type
     * @param int $flow_id
     * @param int $user_id
     * @deprecated
     * @return string
     */
    private function _get_flow_url($flow_type,$flow_id,$user_id=0){
        switch ($flow_type){
            case Const_Flow::FLOW_TYPE_ENTRY:
                $url = Flow_Urls::add_staff($user_id);
                break;
            case Const_Flow::FLOW_TYPE_TRANSFER:
            case Const_Flow::FLOW_TYPE_MONEY:
                    $url = Flow_Urls::transfer().'?flow_id='.$flow_id;
                break;
            case Const_Flow::FLOW_TYPE_DIMISSION:
                $url = Flow_Urls::dimission().'?flow_id='.$flow_id;
                break;
            case Const_Flow::FLOW_TYPE_RECRUIT:
                $url = Flow_Urls::flow_recruitment($flow_id);
                break;
            default:
                break;
        }
        return $url;
    }
    /**
     * 权限能管理的城市
     * @return array
     */
    private function _get_city_list(){
        $proe_bll = new Bll_Hr_Property();
        $city_list = $proe_bll->get_city_list();
        return $city_list;
    }

    private function _get_flow_type(){
        return array(
                Const_Flow::FLOW_TYPE_ENTRY=>'入职',
                Const_Flow::FLOW_TYPE_TRANSFER=>'异动',
                Const_Flow::FLOW_TYPE_DIMISSION=>'离职',
                Const_Flow::FLOW_TYPE_RECRUIT=>'招聘',
                Const_Flow::FLOW_TYPE_REGULAR=>'转正',
                Const_Flow::FLOW_TYPE_RENEW=>'续签',
        );
    }
    /**
     * 动作与流程实际错一个位置
     * @param unknown_type $data
     * @return Ambigous <string>
     */
    private function _show_recruit_stage($data){
        $list = array(
                Const_Flow::PHASE_50=>'一级主管',
                Const_Flow::PHASE_51=>'二级主管',
                Const_Flow::PHASE_52=>'三级主管',
                Const_Flow::PHASE_53=>'四级主管',
                Const_Flow::PHASE_70=>'五级主管',
                Const_Flow::PHASE_71=>'六级主管',
                Const_Flow::PHASE_72=>'七级主管',
                Const_Flow::PHASE_73=>'八级主管',
                Const_Flow::PHASE_74=>'九级主管',
                Const_Flow::PHASE_75=>'十级主管',
                );
        if(!$data['recruit'] && $data['flow_opt']>51){
            return '招聘';
        }
        return $list[$data['flow_opt']];
    }
}